<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="__PUBLIC__/css/jquery-ui-1.8.18.custom.css">
<script src="__PUBLIC__/js/jquery-1.7.1.min.js"></script>
<script src="__PUBLIC__/js/jquery-ui-1.8.18.custom.min.js"></script>
<script>
$(document).ready(function(){
	var edit = $("span[name='edit']");
	edit.click(function(){
		var id = $(this).parents("tr").children('td:first').html();
		var url = "__URL__/getMenuById";
		var data = "id="+id;
		$.post(url,data,function(res){
			$('#url').val('');
			$('#level').val('');
			$('#id').val(res['id']);
			$('#name').val(res['name']);
			$('#sort').val(res['sort']);
			$('#model').val(res['model']);
			$('#action').val(res['action']);
			$('#url').val(res['url']);
			$('#level').val(res['level']);
			$('#status').find("option").each(function(){
				if($(this).val()==res['status']){
					$(this).attr('selected','selected');
				}
			});
			$('#pid').find("option").each(function(){
				if($(this).val()==res['pid']){
					$(this).attr('selected','selected');
				}
			});
		},'json');
	});
	
	$('#pid').change(function(){
		var level = parseInt($('#pid').find("option:selected").attr('level'))+1;
		$('#level').val(level);
	});
	
	var del = $("span[name='del']");
	del.click(function(){
		var id = $(this).parents("tr").children('td:first').html();
		var tips = "确定要删除ID为"+id+"的菜单吗?";
		var sure = confirm(tips);
		if(sure){
			var url = "__URL__/delete";
			var data = "id="+id;
			$.post(url,data,function(res){
				if(res==1){
					alert("删除成功");
					location.reload();
				}else{
					alert("删除失败");
				}
			});
		}
	});
	
	$("reset").click(function(){
		$('#id').val(0);
		$('#url').val('');
		$('#pid').find("option").attr('selected',false);
	});
	
	$("#save").click(function(){
		//如果有id则更新，否则新增
		var actionUrl = "__URL__/insert";
		var pid = parseInt($('#pid').find("option:selected").val());
		var name = $("#name").val();
		var model = $("#model").val();
		var action = $("#action").val();
		var status = parseInt($('#status').find("option:selected").val());
		var sort = $("#sort").val();
		var reg = new RegExp("^\\d+$");
		if(name == ''){
			alert("菜单名不能为空");
			return false;
		}
		if(!reg.test(sort)){
			alert("sort require number");
			return false;
		}
		var level = $("#level").val();
		var data = "name="+name+"&model="+model+"&action="+action+"&level="+level+"&pid="+pid+"&status="+status;
		if(parseInt($("#level").val())==3){
			var url =  $("#model").val() + "/" +$("#action").val();
			$("#url").val(url);
			if(url =="/"){
				alert("请输入模块和操作!");
				return false;
			}
			data += "&url="+url;
		}
		var id = parseInt($("#id").val());
		if(id!=0){
			actionUrl= "__URL__/update";
			data += "&id="+id;
		}
		$.post(actionUrl,data,function(res){
			if(res==1){
				alert("保存成功");
				location.reload();
			}else{
				alert("保存失败");
			}
		});
		
	});
});
</script>
</head>
<body>

<table cellspacing="1"  style="background:black;width:50%;float:left;">
	<tr style="background:white;"><th colspan="8">菜单列表</th></tr>
	<tr style="background:white;"><th>ID</th><th>菜单名</th><th>模块</th><th>操作名</th><th>层级</th><th>排序</th><th>状态</th><th>操作</th></tr>
	<volist name="menus" id="menu">
		<tr style="background:white;">
		<td>{$menu.id}</td>
		<td>{$menu.name}</td>
		<td>{$menu.model}</td>
		<td>{$menu.action}</td>
		<td>{$menu.level}</td>
		<td>{$menu.sort}</td>
		<td><if condition="$menu.status eq 1">正常<else/>禁用</if></td>
		<td><span name="edit">编辑</span>｜<span name="del">删除</span></td></tr>
	</volist>
</table>
<table cellspacing="1" style="background:black;width:49%;float:left;margin-left:1%;">
	<form action="__URL__/insert" method="post">
	<input type="hidden" id="id" value="0" />
			<tr style="background:white;">
				<td>上级菜单:</td><td>
				<select name="pid" id="pid">
					<option value="0" level="0">顶级菜单</option>
					<volist id="t" name="top">
						<option value="{$t.id}" level="{$t.level}">{$t.name}</option>
							<foreach name="t['sub']" item="m">
								<option value="{$m.id}" level="{$m.level}">&nbsp;&nbsp;&nbsp;{$m.name}</option>
							</foreach>
					</volist>
				</select></td>
			</tr>
			<tr style="background:white;"><td>菜单名:</td><td><input name="name" id="name" type="text"/></td></tr>
			<tr style="background:white;"><td>模块名:</td><td><input name="model" id="model" type="text"/></td></tr>
			<tr style="background:white;"><td>操作名:</td><td><input name="action" id="action" type="text"/></td></tr>
			<tr style="background:white;"><td>状态:</td>
				<td>
					<select name="status" id="status">
						<option value="1">正常</option>
						<option value="0">禁用</option>
					</select>
				</td>
			</tr>
			<tr style="background:white;"><td>排序:</td><td><input name="sort" id="sort" type="text" value="0"/></td></tr>
			<tr style="background:white;"><td colspan="2"><input name="submit" id="save" type="button" value="提交"/><input name="reset" id="reset" type="reset" value="重置"/></td></tr>
			<input name="url" id="url" type="hidden" value=""/>
			<input type="hidden" name="level" id="level" value="1"/>
	</form>
</table>
</body>
</html>